////////////////////////////////////////////////////////////////////////////////


*This do-file produces the majority of Appendix C's output, though Figures C.1-C.4 are produced by "8_regressions_jobsecurity.do" because they are illustrations of the main analysis found in the text.


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
*Tables C.1-C.4: Testing the plausibility of different functional forms for the time trend
////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK
cd "$ukdata"

use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.age i.v*##c.age_squared i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##i.male i.v*##ib1.marital i.v*##i.uni i.v*##i.immigrant i.v*##i.nonwhite i.v*##ib1.isco i.v*##ib2001.year* if (insample==1) [pw=relative_xw], a(person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/poisson_marginal_effects_controls.dta") controls

use "temp/poisson_marginal_effects_controls.dta", clear

	drop if year==2018
	recode variance4_controls_0 .=0
	gen dydx_nocontrols=dprob1dx+dprob2dx
	gen dydx_controls=dprob1dx_controls+dprob2dx_controls
	gen se_controls=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	gen ci_hi_controls=dydx_controls+1.96*se_controls
	gen ci_low_controls=dydx_controls-1.96*se_controls
	
*Regress AMEs on unemployment and time trend
merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace urate=urate*100
rename urate unrate
gen t=year-1991

mkspline t1 1.7 t2 = t

*Variable rescaling
replace t = t/10
replace dydx_controls=dydx_controls*100

reg dydx_controls unrate t, vce(robust)
estimates save bhps_controls, replace

reg dydx_controls unrate t, vce(robust)
estimates store bhps_controls
reg dydx_controls unrate t1 t2, vce(robust)
estimates store bhps_controls_spline

replace t = t-2.5
reg dydx_controls unrate t c.t#c.t, vce(robust)
estimates store bhps_controls_quadratic

*Table C.1
esttab bhps_controls bhps_controls_quadratic bhps_controls_spline using "$tables/bhps_t_forms.tex", replace r2 cells(b(fmt(%9.3g) star) se(fmt(%9.3g) par(( )))) keep(unrate t c.t#c.t t1 t2) coeflabels(t "t" c.t#c.t "t^{2}" unrate "Unemployment rate*100" t1 "Linear spline (pre-2008)" t2 "Linear spline (post-2008)") booktab collabels("") mlabels("AME" "AME" "AME") postfoot("\tabnotes{4}{This table displays the results of regressions of the average marginal effects of the year dummies (with controls) on the unemployment rate and various functional forms of time using the BHPS/UKHLS data. In the quadratic functional form, \textit{t} is re-parameterized to be equal to \textit{year} - 2016. Standard errors in parentheses. *p < 0.05, **p < 0.01 ***p < 0.001}") title("UK: Time trend and unemployment rate regression results") width(\hsize)

cd "$ukdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**PART TWO, v1: US (GSS)
cd "$usdata"

use "clean/gss_clean.dta", clear
	
*Year dummies with controls
logit insecure ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital if (insample==1) [pw=wtssall], vce(cluster year)
	margins, dydx(i.year) saving(temp/controls.dta, replace)

use "temp/controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_lb _ci_ub _se_margin) (dydx_controls ci_lower_controls ci_upper_controls se_controls)
	keep year dydx_controls ci_upper_controls ci_lower_controls se_controls
	set obs 22
	replace dydx_controls=0 in 22
	replace ci_upper_controls=0 in 22
	replace ci_lower_controls=0 in 22
	replace year = 2002 in 22
	
*Regress AMEs on unemployment and time trend
drop if year==.
merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace unrate=unrate*100
gen t=year-1978

mkspline t1 3 t2 = t

*Variable rescaling
replace t = t/10
replace dydx_controls=dydx_controls*100
reg dydx_controls unrate t, vce(robust)
estimates save gss_controls, replace

reg dydx_controls unrate t, vce(robust)
estimates store gss_controls
reg dydx_controls unrate t1 t2, vce(robust)
estimates store gss_controls_spline

replace t = t-3.8
reg dydx_controls unrate t c.t#c.t, vce(robust)
estimates store gss_controls_quadratic

*Table C.2
esttab gss_controls gss_controls_quadratic gss_controls_spline using "$tables/gss_t_forms.tex", replace r2 cells(b(fmt(%9.3g) star) se(fmt(%9.3g) par(( )))) keep(unrate t c.t#c.t t1 t2) coeflabels(t "t" c.t#c.t "t^{2}" unrate "Unemployment rate*100" t1 "Linear spline (pre-2008)" t2 "Linear spline (post-2008)") booktab collabels("") mlabels("AME" "AME" "AME") postfoot("\tabnotes{4}{This table displays the results of regressions of the average marginal effects of the year dummies (with controls) on the unemployment rate and various functional forms of time using the GSS data. In the quadratic functional form, \textit{t} is re-parameterized to be equal to \textit{year} - 2016. Standard errors in parentheses. *p < 0.05, **p < 0.01 ***p < 0.001}") title("US (GSS): Time trend and unemployment rate regression results") width(\hsize)

**PART TWO, v2: US (HRS)
cd "$usdata"

use "clean/hrs_clean.dta", clear

reg insecurity ib2002.year i.parttime age age_squared i.male i.union i.immigrant i.uni i.nonwhite ib5.marital i.industry if (insample==1) [pw=wtresp], vce(cluster year)
	margins, dydx(i.year) saving(temp/controls_hrs.dta, replace)
	
use "temp/controls_hrs.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx_controls ci_upper_controls ci_lower_controls
	set obs 12
	replace dydx_controls=0 in 12
	replace ci_upper_controls=0 in 12
	replace ci_lower_controls=0 in 12
	replace year = 2002 in 12

*Regress AMEs on unemployment and time trend
drop if year==.
merge 1:1 year using "clean/unrate.dta"
keep if _merge==3
drop _merge
replace unrate=unrate*100
gen t=year-1994

mkspline t1 1.4 t2 = t

*Variable rescaling
replace t = t/10
replace dydx_controls=dydx_controls*100
reg dydx_controls unrate t, vce(robust)
estimates save hrs_controls, replace

reg dydx_controls unrate t, vce(robust)
estimates store hrs_controls
reg dydx_controls unrate t1 t2, vce(robust)
estimates store hrs_controls_spline

replace t = t-1.2
reg dydx_controls unrate t c.t#c.t, vce(robust)
estimates store hrs_controls_quadratic

*Table C.3
esttab hrs_controls hrs_controls_quadratic hrs_controls_spline using "$tables/hrs_t_forms.tex", replace r2 cells(b(fmt(%9.3g) star) se(fmt(%9.3g) par(( )))) keep(unrate t c.t#c.t t1 t2) coeflabels(t "t" c.t#c.t "t^{2}" unrate "Unemployment rate*100" t1 "Linear spline (pre-2008)" t2 "Linear spline (post-2008)") booktab collabels("") mlabels("AME" "AME" "AME") postfoot("\tabnotes{4}{This table displays the results of regressions of the average marginal effects of the year dummies (with controls) on the unemployment rate and various functional forms of time using the HRS data. In the quadratic functional form, \textit{t} is re-parameterized to be equal to \textit{year} - 2016. Standard errors in parentheses. *p < 0.05, **p < 0.01 ***p < 0.001}") title("US (HRS): Time trend and unemployment rate regression results") width(\hsize)

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**PART THREE: Germany
cd "$germanydata"

use "clean/soep_clean.dta", clear

*Year dummies with controls
logit insecure ib2001.year i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni i.ddr if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_controls.dta, replace)

use "temp/soep_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_lb _ci_ub _se) (dydx_controls ci_lower_controls ci_upper_controls se_controls)
	keep year dydx_controls ci_upper_controls ci_lower_controls se_controls
	set obs 34
	replace dydx_controls=0 in 34
	replace ci_upper_controls=0 in 34
	replace ci_lower_controls=0 in 34
	replace year = 2001 in 34 
	sort year

*Regress AMEs on unemployment rate and linear time trend
cd "$germanydata"
merge 1:1 year using "clean/unemployment.dta"
rename urate unrate
keep if _merge==3
drop _merge
replace unrate=unrate*100
gen t=year-1985
drop if year==1984

*Variable rescaling
replace t = t/10
replace dydx_controls=dydx_controls*100

mkspline t1 2 t2 = t
mkspline t3 2.3 t4 = t

reg dydx_controls unrate t, vce(robust)
estimates save soep_controls, replace

reg dydx_controls unrate t, vce(robust)
estimates store soep_controls
reg dydx_controls unrate t1 t2, vce(robust)
estimates store soep_controls_spline1
reg dydx_controls unrate t3 t4, vce(robust)
estimates store soep_controls_spline2

drop t3 t4
gen t3=t
gen post=(year>=2005)
gen t4=post*t3
reg dydx_controls unrate post t3 t4, vce(robust) 
estimates store soep_controls_break

replace t = t-3.1
reg dydx_controls unrate t c.t#c.t, vce(robust)
estimates store soep_controls_quadratic

*Table C.4
esttab soep_controls soep_controls_quadratic soep_controls_spline1 soep_controls_spline2 soep_controls_break using "$tables/soep_t_forms.tex", replace r2 cells(b(fmt(%9.3g) star) se(fmt(%9.3g) par(( )))) keep(unrate t c.t#c.t t1 t2 t3 t4 post) coeflabels(t "t" c.t#c.t "t^{2}" unrate "Unemployment rate*100" t1 "Linear spline (pre-2008)" t2 "Linear spline (post-2008)" t3 "Linear spline/trend break (pre-2005)" t4 "Linear spline/trend break (post-2005)" post "Post 2005") booktab collabels("") mlabels("AME" "AME" "AME" "AME" "AME") postfoot("\tabnotes{6}{This table displays the results of regressions of the average marginal effects of the year dummies (with controls) on the unemployment rate and various functional forms of time using the SOEP data. In the quadratic functional form, \textit{t} is re-parameterized to be equal to \textit{year} - 2016. Column (4) uses a linear spline, while Column (5) uses a trend break. Standard errors in parentheses. *p < 0.05, **p < 0.01 ***p < 0.001}") title("Germany: Time trend and unemployment rate regression results") width(\hsize)

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

////////////////////////////////////////////////////////////////////////////////

**Main regressions with individual FEs (Figures C.5-C.7)

////////////////////////////////////////////////////////////////////////////////
**PART ONE: US (HRS)
cd "$usdata"

*Without controls
use "clean/hrs_clean.dta", clear
xtset hhidpn year

reghdfe insecurity ib2002.year if (insample==1) [pw=wtresp], a(hhidpn) vce(cluster year)
	margins, dydx(i.year) saving(temp/nocontrols_hrs_fe.dta, replace)

*Year dummies with controls
reghdfe insecurity ib2002.year i.semp i.parttime age age_squared i.union ib5.marital if (insample==1) [pw=wtresp], a(hhidpn) vce(cluster year)
	margins, dydx(i.year) saving(temp/controls_hrs_fe.dta, replace)

*Plot
	use "temp/nocontrols_hrs_fe.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
tempfile nocontrols
save `nocontrols'

use "temp/controls_hrs_fe.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.2
	replace year=year-.2
	set obs 12
	replace dydx_nocontrols=0 in 12
	replace dydx_controls=0 in 12
	replace ci_upper_controls=0 in 12
	replace ci_lower_controls=0 in 12
	replace ci_upper_nocontrols=0 in 12
	replace ci_lower_nocontrols=0 in 12
	replace year=2001.8 in 12
	replace year_controls=2002.2 in 12
	sort year
	
*Figure C.5
cd "$usgraphics"

twoway (scatter dydx_nocontrols year, connect(direct) msize(small)) (scatter dydx_controls year_controls, connect(direct) msize(small)) (rcap ci_upper_nocontrols ci_lower_nocontrols year, msize(0) lcolor(navy)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(maroon)), xlabel(1994(2)2016, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) graphregion(color(white)) bgcolor(white) title("Marginal effects of year dummies on Pr(Insecure)" "US 1994-2016") yline(0, lcolor(black*2)) xline(2002, lcolor(red*1.5) lpattern(dash)) ytitle("Marginal effect") note("Individual fixed effects included in regression")

graph export "hrs_year_dummies_fe.png", as(png) name("Graph") replace

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**UK
cd "$ukdata"

*With demographic controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##i.temp i.v*##i.parttime i.v*##c.jobtenure i.v*##i.semp_boss i.v*##i.semp i.v*##i.union i.v*##ib1.marital i.v*##ib1.isco i.v*##ib2001.year* if (insample==1) [pw=relative_xw], a(id person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/poisson_marginal_effects_controls_fe.dta") controls

*Without controls
use "clean/uk_poisson_reverse.dta", clear

ppmlhdfe choice i.v*##ib2001.year if (insample==1) [pw=relative_xw], a(id person_year_id) vce(cluster year) d

poisson_year_mfx, save("temp/poisson_marginal_effects_nocontrols_fe.dta")

*Produce plot of marginal effects, controls and no controls
use "temp/poisson_marginal_effects_nocontrols_fe.dta", clear

merge 1:1 year using "temp/poisson_marginal_effects_controls_fe.dta"
	drop _merge
	recode variance4_controls_0 .=0
	gen dydx_nocontrols=dprob1dx+dprob2dx
	gen dydx_controls=dprob1dx_controls+dprob2dx_controls
	gen se_controls=sqrt(variance1_controls+variance2_controls+variance1_controls_0+variance2_controls_0)
	gen se_nocontrols=sqrt(variance1_nocontrols+variance2_nocontrols+variance1_2001+variance2_2001)
	gen ci_hi_controls=dydx_controls+1.96*se_controls
	gen ci_low_controls=dydx_controls-1.96*se_controls
	gen ci_hi_nocontrols=dydx_nocontrols+1.96*se_nocontrols
	gen ci_low_nocontrols=dydx_nocontrols-1.96*se_nocontrols
	gen year_controls=year+.2
	replace year=year-.2
	
*Figure C.6
graph twoway (scatter dydx_nocontrols year, msize(small) connect(direct)) (scatter dydx_controls year_controls, msize(small) connect(direct)) (rcap ci_hi_nocontrols ci_low_nocontrols year, msize(0) lcolor(navy)) (rcap ci_hi_controls ci_low_controls year_controls, msize(0) lcolor(maroon)), graphregion(color(white)) bgcolor(white) title("Implied marginal effects of year dummies on Pr(insecure)" "BHPS and US, 1991-2018 (net of individual fixed effects)") xtitle("Year") ytitle("Marginal effect") xlabel(1991/2017, angle(45)) yline(0, lcolor(black)) xline(2001, lpattern(dash) lcolor(red)) legend(label(1 "No controls") label(2 "Controls") label(3 "95% CI") label(4 "95% CI"))

graph export "$ukgraphics/year_fes_poisson_fe.png", as(png) name("Graph") replace

cd "$ukdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

**Germany

cd "$germanydata"

use "clean/soep_clean.dta", clear

*Year dummies without controls, FE
xtset pid year
xtlogit insecure ib2001.year if insample==1, fe vce(oim)
	margins, dydx(i.year) saving(temp/soep_nocontrols_fe.dta, replace)
	
*Year dummies with controls, FE
xtlogit insecure ib2001.year i.temp i.parttime i.semp i.marginal ib3.marital ib1.isco i.uni c.jobtenure if insample==1, fe vce(oim)
	margins, dydx(i.year) saving(temp/soep_controls_fe.dta, replace)
	
use "temp/soep_nocontrols_fe.dta", clear
	keep _deriv _margin _ci*
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx ci_upper ci_lower)
tempfile nocontrols
save `nocontrols'

use "temp/soep_controls_fe.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx ci_upper ci_lower dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.15
	replace year=year-.15
	set obs 34
	replace dydx=0 in 34
	replace dydx_controls=0 in 34
	replace ci_upper_controls=0 in 34
	replace ci_lower_controls=0 in 34
	replace ci_upper=0 in 34
	replace ci_lower=0 in 34
	replace year=2000.85 in 34
	replace year_controls=2001.15 in 34
	sort year

*Figure C.7
twoway (scatter dydx year, connect(direct) msize(small)) (scatter dydx_controls year_controls, connect(direct) msize(small)) (rcap ci_upper ci_lower year, msize(0) lcolor(navy)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(maroon)), graphregion(color(white)) bgcolor(white) xlabel(1985(2)2018, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Insecure)" "(net of individual fixed effects)") yline(0, lcolor(black*2)) xline(2001, lcolor(red*1.5) lpattern(dash)) ytitle("Marginal effect")

graph export "$germanygraphics/soep_year_dummies_fe.png", as(png) name("Graph") replace

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

